package com.sne.sys.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.common.entity.Result;
import com.sne.sys.dto.SysRoleDTO;
import com.sne.sys.dto.SysRoleParamDTO;
import com.sne.sys.entity.SysPost;
import com.sne.sys.entity.SysRole;
import com.sne.sys.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author fanghh
 * @since 2020-01-03
 */
@Slf4j
@RestController
@RequestMapping("/sys/sys-role")
@Api(tags = "角色")
public class SysRoleController {

    @Autowired
    private ISysRoleService service;

    @PreAuthorize("hasAuthority('sys:role:page')")
    @ApiOperation(value = "角色列表")
    @PostMapping("/page")
    public Result page(Page page, @RequestBody SysRoleParamDTO role) {
        return Result.success(service.findPage(page,role));
    }

    @PreAuthorize("hasAuthority('sys:role:save')")
    @ApiOperation(value = "保存角色")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SysRoleDTO role) {
        if (!service.checkUnique(role)) {
            return Result.failed("角色编号已存在",null);
        }
        return Result.success(service.saveRole(role));
    }

    @PreAuthorize("hasAuthority('sys:role:update')")
    @ApiOperation(value = "更新岗位")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody SysRoleDTO role) {
        if (!service.checkUnique(role)) {
            return Result.failed("角色编号已存在",null);
        }
        return Result.success(service.updateRole(role));
    }

    @PreAuthorize("hasAuthority('sys:role:delete')")
    @ApiOperation(value = "删除岗位")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(service.delete(id));
    }

    @ApiOperation(value = "获取岗位")
    @GetMapping("/find/{id}")
    public Result find(@PathVariable("id") Long id) {
        return Result.success(service.find(id));
    }

    @PreAuthorize("hasAuthority('sys:role:deleteIds')")
    @ApiOperation(value = "批量删除岗位")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody @Size(min = 1,message = "ids不能为空") List<Long> ids) {
        return Result.success(service.deleteIds(ids));
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/list")
    public Result list(){
        return Result.success(service.list(Wrappers.<SysRole>query()
                .lambda().eq(SysRole::getState,true)));
    }

}

